﻿// CRightPage.cpp: 实现文件
//

#include "pch.h"
#include "管理系统.h"
#include "afxdialogex.h"
#include "CRightPage.h"
#include "管理系统Dlg.h"




// CRightPage 对话框

IMPLEMENT_DYNAMIC(CRightPage, CDialogEx)

CRightPage::CRightPage(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_RIGHT_Page, pParent)
{

}

CRightPage::~CRightPage()
{
}

void CRightPage::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_LIST2, m_scoreList);
}


BEGIN_MESSAGE_MAP(CRightPage, CDialogEx)
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_DESTROY()
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &CRightPage::OnClickList1)
	ON_NOTIFY(NM_CLICK, IDC_LIST2, &CRightPage::OnClickList2)
END_MESSAGE_MAP()


// CRightPage 消息处理程序


BOOL CRightPage::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CRect rect;
	GetClientRect(&rect);

	pDC->FillSolidRect(&rect, RGB(255, 255, 255));
	return true;



	//return CDialogEx::OnEraseBkgnd(pDC);
}


void CRightPage::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码

	if (m_list.GetSafeHwnd())
	{
		CRect rect;
		GetClientRect(&rect);

		m_list.MoveWindow(&rect);
	}
	if (m_scoreList.GetSafeHwnd())
	{
		CRect rect;
		GetClientRect(&rect);

		m_scoreList.MoveWindow(&rect);
	}
}


BOOL CRightPage::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	//1.m_list初始化：
	m_list.ModifyStyle(0,
		LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_VSCROLL | WS_BORDER);
	//设置列表风格为整行选取：
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	//设置列表风格为表格类型：
	//m_list.SetExtendedStyle(LVS_EX_GRIDLINES & LVS_EX_FULLROWSELECT);
	// 整行选取的风格和 表格的风格 在一起好像不兼容，有表格就不能整行选取

	CRect rect;
	GetClientRect(&rect);

	m_list.MoveWindow(&rect);
	
	m_list.InsertColumn(0, L"学号", LVCFMT_LEFT, 150);
	m_list.InsertColumn(1, L"姓名", LVCFMT_LEFT, 100);
	m_list.InsertColumn(2, L"性别", LVCFMT_LEFT, 50);
	m_list.InsertColumn(3, L"兴趣", LVCFMT_LEFT, 250);
	m_list.InsertColumn(4, L"年龄", LVCFMT_LEFT, 50);
	m_list.InsertColumn(5, L"年级", LVCFMT_LEFT, 100);
	m_list.InsertColumn(6, L"专业", LVCFMT_LEFT, 100);
	m_list.InsertColumn(7, L"班级", LVCFMT_LEFT, 100);
	m_list.InsertColumn(8, L"电话", LVCFMT_LEFT, 200);
	m_list.InsertColumn(9, L"地址", LVCFMT_LEFT, 500);








	//2.m_scoreList初始化;
	m_scoreList.ModifyStyle(0,
		LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_VSCROLL | WS_BORDER);
	//设置列表风格为整行选取：
	m_scoreList.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	m_scoreList.MoveWindow(&rect);

	m_scoreList.InsertColumn(0, L"学号", LVCFMT_LEFT, 150);
	m_scoreList.InsertColumn(1, L"姓名", LVCFMT_LEFT, 100);
	m_scoreList.InsertColumn(2, L"年级", LVCFMT_LEFT, 100);
	m_scoreList.InsertColumn(3, L"专业", LVCFMT_LEFT, 100);
	m_scoreList.InsertColumn(4, L"班级", LVCFMT_LEFT, 100);
	m_scoreList.InsertColumn(5, L"操作系统", LVCFMT_LEFT, 150);
	m_scoreList.InsertColumn(6, L"微机原理", LVCFMT_LEFT, 150);
	m_scoreList.InsertColumn(7, L"c语言程序设计", LVCFMT_LEFT, 150);
	m_scoreList.InsertColumn(8, L"cpp程序设计", LVCFMT_LEFT, 150);



	m_list.ShowWindow(SW_SHOW);
	m_scoreList.ShowWindow(SW_HIDE);

	OnloadStudentInfo();


	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


// 由左控件向右控价传送学生信息
void CRightPage::insertStudent(Student stu)
{
	// TODO: 在此处添加实现代码.

	int nCnt = m_list.GetItemCount();
	CString  str;
	str.Format(L"%d", stu.nStuNum);
	m_list.InsertItem(nCnt, str);

	m_list.SetItemText(nCnt, 1, stu.stuName);
	m_list.SetItemText(nCnt, 2, stu.stuSex);
	m_list.SetItemText(nCnt, 3, stu.stuInterest);

	str.Format(L"%d", stu.stuAge);
	m_list.SetItemText(nCnt, 4, str);

	m_list.SetItemText(nCnt, 5, stu.stuGrage);
	m_list.SetItemText(nCnt, 6, stu.stuMajor);
	m_list.SetItemText(nCnt, 7, stu.stuClass);
	m_list.SetItemText(nCnt, 8, stu.stuTel);
	m_list.SetItemText(nCnt, 9, stu.stuAddress);
}

void CRightPage::insertStudent_score(StudentScore stuscore)
{
	// TODO: 在此处添加实现代码.

	int nCnt = m_scoreList.GetItemCount();
	CString  str;
	str.Format(L"%d", stuscore.nStuNum);
	m_scoreList.InsertItem(nCnt, str);

	m_scoreList.SetItemText(nCnt, 1, stuscore.stuName);
	m_scoreList.SetItemText(nCnt, 2, stuscore.stuGrage);
	m_scoreList.SetItemText(nCnt, 3, stuscore.stuMajor);
	m_scoreList.SetItemText(nCnt, 4, stuscore.stuClass);
	str.Format(L"%.2lf", stuscore.m_score_czxt);
	m_scoreList.SetItemText(nCnt, 5, str);
	str.Format(L"%.2lf", stuscore.m_score_wjyl);
	m_scoreList.SetItemText(nCnt, 6, str);
	str.Format(L"%.2lf", stuscore.m_score_wjyl);
	m_scoreList.SetItemText(nCnt, 7, str);
	str.Format(L"%.2lf", stuscore.m_score_wjyl);
	m_scoreList.SetItemText(nCnt, 8, str);
}


void CRightPage::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: 在此处添加消息处理程序代码


	//1.保存学生信息（数据）：

	//打开文件：
	CFile file;
	bool isDone = file.Open(L".\\stu.txt",
		CFile::modeCreate | CFile::modeWrite | CFile::typeUnicode);
	if (!isDone)
	{
		MessageBox(L"保存学生信息失败");
		return;
	}

	
	for (int i = 0; i < m_list.GetItemCount(); i++)
	{
		Student stu = { 0 };

		//将list控件中的学生信息 赋值到结构体stu中:
		stu.nStuNum = _wtoi(m_list.GetItemText(i, 0));
		wcscpy(stu.stuName, m_list.GetItemText(i, 1));
		wcscpy(stu.stuSex, m_list.GetItemText(i, 2));
		wcscpy(stu.stuInterest, m_list.GetItemText(i, 3));

		stu.stuAge = _wtoi( m_list.GetItemText(i, 4));
		wcscpy(stu.stuGrage, m_list.GetItemText(i, 5));
		wcscpy(stu.stuMajor, m_list.GetItemText(i, 6));
		wcscpy(stu.stuClass, m_list.GetItemText(i, 7));
		wcscpy(stu.stuTel, m_list.GetItemText(i, 8));
		wcscpy(stu.stuAddress, m_list.GetItemText(i, 9));

		file.Write(&stu, sizeof(stu));

	}
	file.Close();


	//2.保存学生成绩信息（数据）：
	CFile file1;
	isDone = file1.Open(L".\\stuScore.txt",
		CFile::modeCreate | CFile::modeWrite | CFile::typeUnicode);
	if (!isDone)
	{
		MessageBox(L"保存学生成绩信息失败");
		return;
	}
	for (int i = 0; i < m_scoreList.GetItemCount(); i++)
	{
		StudentScore stu1 = { 0 };

		//将list控件中的学生信息 赋值到结构体stu中:
		stu1.nStuNum = _wtoi(m_scoreList.GetItemText(i, 0));
		wcscpy(stu1.stuName, m_scoreList.GetItemText(i, 1));
		wcscpy(stu1.stuGrage, m_scoreList.GetItemText(i, 2));
		wcscpy(stu1.stuMajor, m_scoreList.GetItemText(i, 3));
		wcscpy(stu1.stuClass, m_scoreList.GetItemText(i, 4));
		stu1.m_score_czxt = _wtof(m_scoreList.GetItemText(i, 5));
		stu1.m_score_wjyl = _wtof(m_scoreList.GetItemText(i, 6));
		stu1.m_score_c = _wtof(m_scoreList.GetItemText(i, 7));
		stu1.m_score_cpp = _wtof(m_scoreList.GetItemText(i, 8));
		

		file1.Write(&stu1, sizeof(stu1));

	}
	file1.Close();


}



// 从文件中读取加载学生信息
void CRightPage::OnloadStudentInfo()
{
	// TODO: 在此处添加实现代码.


	//1加载学生信息数据：
	//打开文件：
	CFile file;
	bool isDone = file.Open(L".\\stu.txt",
		 CFile::modeRead | CFile::typeUnicode);
	if (!isDone)
	{
		MessageBox(L"打开学生信息失败");
		return;
	}

	//重置文件指针到文件开头
	file.SeekToBegin();


	//读取数据：
	Student stu = { 0 };

	while (file.Read(&stu, sizeof(stu)))
	{
		insertStudent(stu);
	}
	file.Close();






	//2.加载学生成绩数据：
	CFile file1;
	isDone = file1.Open(L".\\stuScore.txt",
		CFile::modeRead | CFile::typeUnicode);
	if (!isDone)
	{
		MessageBox(L"打开学生成绩信息失败");
		return;
	}

	//重置文件指针到文件开头
	file1.SeekToBegin();


	//读取数据：
	StudentScore stu1 = { 0 };

	while (file1.Read(&stu1, sizeof(stu1)))
	{
		insertStudent_score(stu1);
	}
	file1.Close();


}






//获取焦点信息，传给left控件中：
void CRightPage::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码


	//选中哪一行：
	//1.学生信息的焦点:
	POSITION d_pos = m_list.GetFirstSelectedItemPosition();
	int d_nSel = m_list.GetNextSelectedItem(d_pos);

	

	C管理系统Dlg* pMainDlg = (C管理系统Dlg*)AfxGetMainWnd();

	pMainDlg->m_leftPage.d_Pos = d_nSel;


	pMainDlg->m_leftPage.m_list = &m_list;
	pMainDlg->m_leftPage.m_scoreList = &m_scoreList;


	*pResult = 0;
}





void CRightPage::OnClickList2(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码



	//2.学生成绩信息的焦点:
	POSITION s_pos = m_scoreList.GetFirstSelectedItemPosition();
	int s_nSel = m_list.GetNextSelectedItem(s_pos);

	C管理系统Dlg* pMainDlg = (C管理系统Dlg*)AfxGetMainWnd();

	pMainDlg->m_leftPage.s_Pos = s_nSel;

	*pResult = 0;
}
